<?php
require APPPATH.'libraries/ApiController.php';
class adminrole extends ApiController {
	public function __construct()
	{
		parent::__construct();
	}
	
	public function list(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["query",false,"角色名称",""]],'post');
			$this->db->where("shopid",0);
			$list=$this->db->get("admin_role")->result_array();
			
			$this->db->where("shopid",$token["shopid"]);
			$list1=$this->db->get("admin_role")->result_array();
			
	
			$list=array_merge($list,$list1);
			
			$this->WRITE("code",1);
			$this->WRITE("list",$list);
			$this->RESPONSE();
		}
	}
	
	public function get_auths(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["roleid",true,"会员ID"]],'post');
		
			//获取该角色组权限
			$this->db->where("roleid",$data["roleid"]);
			$authids=$this->db->select("ifnull(roleauth,'') as roleauth")->get("admin_role")->row_array()["roleauth"];
			$auths=$authids==""?[]:explode(",",$authids);
			//var_dump($auths);
			$this->db->order_by("group",'desc');
			$this->db->order_by("order",'desc');
			$list=$this->db->get("page")->result_array();
			for($i=0;$i<count($list);$i++){
				if(in_array($list[$i]["pageid"],$auths)){
					$list[$i]["checked"]=true;
				}else{
					$list[$i]["checked"]=false;
				}
			}
			$this->WRITE("code",1);
			$this->WRITE("auths",$auths);
			$this->WRITE("list",$list);
			$this->RESPONSE();
		}
	}
	
	public function update_auths(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["roleid",true,"会员ID"],
			["auths",false,"权限列表",""]],'post');
			
			$data["roleauth"]=$data["auths"];
			unset($data["auths"]);
			$this->db->where("roleid",$data["roleid"]);
			$this->db->update("admin_role",$data);
			
			$this->WRITE("code",1);
			$this->WRITE("msg","更新成功!");
			$this->RESPONSE();
		}
	}
	
}